package com.sinog.biz.util;

import com.sinog.core.util.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @desc 工作台 批量导出搜索条件返回
 * @author lk
 * @date -
 */
public class ConditionUtils {

    /**
     * 封装 搜索条件
     * @param map 前台传值
     * @param account 登录用户的账号
     * @param redisTemplate redisTemplate
     */
    public static void searchConditions(Map<String,Object> map,String account,RedisTemplate redisTemplate) {
        Map<String,String> searchMap = new HashMap<>(16);
        Set<Map.Entry<String,Object>> entries = map.entrySet();
        for(Map.Entry<String,Object> entry : entries) {
            if("qitaKey".equals(entry.getKey())) {
                List<String> qitaKeyStr = (List<String>)entry.getValue();
                if(CollectionUtils.isNotEmpty(qitaKeyStr)) {
                    searchMap.put(entry.getKey(),StringUtils.join(qitaKeyStr.toArray(),","));
                }
            } else if(ObjectUtils.isNotEmpty(entry.getKey())) {
                searchMap.put(entry.getKey(),entry.getValue().toString());
            }
        }
        redisTemplate.opsForHash().put(account,System.currentTimeMillis(),searchMap);
        redisTemplate.expire(account,5000,TimeUnit.SECONDS);
    }

    /**
     * 将筛选条件放入到redis中
     * @param searchMap searchMap
     */
    public static void fzSearchKey(Map<String,String> searchMap,RedisTemplate redisTemplate) {
        redisTemplate.opsForHash().put(searchMap.get("account"),System.currentTimeMillis(),searchMap);
        redisTemplate.expire(searchMap.get("account"),5000,TimeUnit.SECONDS);
    }
}